<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript" src='jquery-1.7.2.js'></script>
<script src="calendar_beans_v2.0_hide.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var nx=0;
var max=500;
var min=50;	
	$(document).ready(function(){
		  $('#quantity').attr('placeholder','50EA, MIN:50 MAX:'+max);
		/* 
		  * workorder id를 생성해준다.
		  */
		  $("#work_order_id").click(function(){
				$.ajax({
					type:"post",
					url:"workorder.do",
					data:"command=getWorkOrderIdandLastSerialNo",
					dataType:"json",
					success:function(data){
						nx=parseInt(data.wo_map.sno_max,10);
						var wo_last= data.wo_map.last_wo_id.split("_");
						var wo_cu=data.wo_map.wo_id.split("_");	
						var last_id=wo_last[1];
						var cu_id=wo_cu[1];
						if(nx===9999){
							if(last_id!==cu_id){
								$("#work_order_id").val(data.wo_map.wo_id);	
							}else{
								$("#work_order_id").val(data.wo_map.wo_id);	
								check_sno(nx,data.wo_map.wo_id);
							}
						}else{
							$("#work_order_id").val(data.wo_map.wo_id);	
							check_sno(nx,data.wo_map.wo_id);
						}
					}// success
				});// ajax
			});//click
		/*
		  * quantity 50개씩 최소 50 최대 200
		  */
		$("#serial_no_min").click(function(){
			// quantity는 최대 100까지
			if(isNaN($("#quantity").val())){
				alert("Please insert Number!");
				$("#quantity").val("");
				$("#quantity").focus();
			}else{
			if($("#quantity").val()===""){
				alert("Please insert Number!");
				$("#quantity").focus();
			}else{
				var quantity = parseInt($("#quantity").val());
				if(quantity<min){
					alert("최소 "+min+"개 이상 주문 가능");
					$("#quantity").focus();
					$("#quantity").val("");
					return false;
				}else if(quantity%min!==0){
					alert("주문단위:"+min+", min:"+min+", max="+max);
					$("#quantity").focus();
					$("#quantity").val("");
					return false;
				}else if(quantity>max){
					alert("최대 "+max+"개까지 주문 가능");
					$("#quantity").focus();
					$("#quantity").val("");
					return false;
				}
				$.ajax({
					type:"post",
					url:"workorder.do",
					data:"command=getStartandEndSerialNo&quentity="+$("#quantity").val(),
					dataType:"json",
					success:function(data){
						$("#serial_no_min").val(data.sno_map.str_sno); 
						$("#serial_no_max").val(data.sno_map.end_sno);
					},// success,
					error:function(){
						alert("error");
					}
				});// ajax
			}//if-else
			}
		});//click
		$("#saveBtn").click(function(){
			if($("#work_order_id").val()===""){
				alert("Please insert WORK ORDER ID");
				return  false;
			}
			if($("#quantity").val()===""){
				alert("Please insert QUANTITY");
				return  false;			
			}
			if($("#serial_no_min").val()===""){
				alert("Please input SERIAL NO");
				return  false;
			}
			  // 주문전에 재고량 확인
			  // 재고 부족하면 워커오더 이동 후 재고 등록 페이지로 이동
			  
			  // 재고 확인  100개 주문시 얼음 30,000, 팥 20,00 토핑 4,000 용기 100ea
			  $.ajax({
				  
				  type:"post",
				  url:"workorder.do",
				  data:"command=checkInventorybyOrderQuantity&quantity="+$("#quantity").val(),
				  dataType:"json",
				  success:function(data){
					  if(data.reMap.flag==="false"){
						  var c=confirm("["+data.reMap.msg+"]"+"재고 주문 후 등록 가능합니다. ");
						  if(c){
							  location.href="wip.do?command=main&item_id_ice="+data.reMap.ivo.item_id_ice+"&item_id_rb="+data.reMap.ivo.item_id_rb+"&item_id_tp="+data.reMap.ivo.item_id_tp+"&item_id_pc="+data.reMap.ivo.item_id_pc;
						  }
					  }else{
						  location.href="workorder.do?"+$("#WorkorderForm").serialize();
					  }
				}
			  });
		});
	});// ready
function check_sno(nx,wo_id){
	var x1= parseInt(9999);
	var x2= parseInt(9949);
	var x3= parseInt(9899);
	var x4= parseInt(9849);
	var x5= parseInt(9799);
	if(nx!=x1){
		if(nx===x5 || nx===x4|| nx===x3 ||nx===x2){
			var a= x1-parseInt(nx);
			max=a; 
		}
	}else{		
		alert("일일 주문량 초과");
		$("#work_order_id").val("일일 주문량 초과");	
		$("#quantity").val("일일 주문량 초과");	
		$("#serial_no_min").val("일일 주문량 초과");	
		$("#serial_no_max").val("일일 주문량 초과");	
		$("#work_order_id").off();
		$("#quantity").off();
		$("#serial_no_min").off();
		$("#saveBtn").hide();
	}  
}
</script>
<c:choose>
<c:when test="${sessionScope.vo!=null }">
<p class="title">Work Order<p>
<table border="1" cellspacing='0' width="800px">	
	<tr>
		<td>WorkOrder</td>		<td>Quantity</td>		<td>Starting Serial </td>		<td>Ending Serial</td>
		<td rowspan="2"><input type="button" id="saveBtn"  value="Save" style="height: 40px; width: 70px"></td>
	</tr>
	<tr>
	<form id="WorkorderForm">
	<input type="hidden" name="command" value="setWorkOrderInfo" >
		<td><input type="text" id="work_order_id" name="work_order_id" " placeholder="CLICK HERE" readonly="readonly" size="23"></td>
		<td><input type="text" id="quantity" name="quantity" placeholder="" size="23"></td>
		<td><input type="text" id="serial_no_min" name="serial_no_min" placeholder="CLICK HERE" readonly="readonly" size="23"></td>
		<td><input type="text" id="serial_no_max" name="serial_no_max" readonly="readonly" size="23"></td>
	</form>
	</tr>
</table>
<hr>
<p class="title">WorkOrder List</p>
<div id="list">
<table border="1" cellspacing='0' width="800px">
	<thead>
		<tr>
		<td>no</td><td>WorkOrder</td>	<td>Quantity</td>		<td>Starting Serial </td>		<td>Ending Serial</td><td>DATE</td>
		<td>Complete</td>
		</tr>
	</thead>
	<tbody id="wo_list">
		<c:forEach items="${requestScope.list.list }" var="list" >
		<tr>
		<td>${list.RN }</td>
		<td>${list.WORK_ORDER_ID }</td>
		<td>${list.QUANTITY}</td>
		<td>${list.SERIAL_NO_MIN}</td>
		<td>${list.SERIAL_NO_MAX}</td>
		<td>${list.DATE_TIME}</td>
		<td>
			<c:choose>
				<c:when test="${list.COMPLETE=='Y'}">
				<p class="y">COMPLETE</p>
				</c:when>
				<c:otherwise>
				<p class="n">STANDBY</p>
				</c:otherwise>
			</c:choose>
		</td>
		</tr>
		</c:forEach>
	</tbody>
</table>
</div>
<div id="paging">
	<!-- 페이징 처리 -->	
	 <c:if test="${requestScope.list.pagingBean.previousPageGroup}">
	 	<a href="workorder.do?command=main&page=${requestScope.list.pagingBean.startPageOfPageGroup}">
 		<img src="img/left_arrow_btn.gif"></a>
 	</c:if> &nbsp;&nbsp;
	<c:forEach var="i" begin="${requestScope.list.pagingBean.startPageOfPageGroup}" end="${requestScope.list.pagingBean.endPageOfPageGroup}">
 		<c:choose>
 			<c:when test="${requestScope.list.pagingBean.nowPage!=i}">
				<a href="workorder.do?command=main&page=${i}">${i}</a>
			</c:when>
			<c:otherwise><u>${i}</u></c:otherwise>
		</c:choose>
	</c:forEach>&nbsp;&nbsp;
	<c:if test="${requestScope.list.pagingBean.nextPageGroup}">
  		<a href="workorder.do?command=main&page=${requestScope.list.pagingBean.endPageOfPageGroup}">
 			<img src="img/right_arrow_btn.gif"></a> 
 	</c:if>	 	
</div>
</c:when>
<c:otherwise>
<script>
	alert("로그인 후 사용가능");
	location.href="index.jsp";
</script>
</c:otherwise>

</c:choose>